import { ColumnType } from 'antd/lib/table';
import { EAddressSchema } from "@src/components/schema/address"
import { Space, message, Tooltip } from 'antd';
import style from './index.module.less'
import { Dispatch } from 'redux';
import { addWindow } from "@src/redux/actions/tab"
import { awaitWrap, confirmAction, formatDate } from '@src/assets/js/tool'
import { updateRequest } from './ajax'
import truePng from '@imgs/true.png'
import falsePng from '@imgs/false.png'
import classNames from 'classnames';

async function updateHandle (d: {[k: string]: any}, refreshFn: () => void) {
  const h = message.loading('作废中', 120)
  const [e] = await awaitWrap(updateRequest(d))
  h()
  if (e === null) {
    refreshFn()
  }
}

 // 获取表格字段配置
 export function getColumns (
  caseid: string,
  editable: boolean,
  setModifyData: (v: EAddressSchema) => void,
  setVisible: (v: boolean) => void,
  refreshFn: () => void
): Array<ColumnType<EAddressSchema>> {
  const list: Array<ColumnType<EAddressSchema>> = [
    {
      title: '收件人',
      dataIndex: 'contract',
      key: 'contract',
      render: (v, l) => {
        return v || l.structure
      }
    },
    {
      title: '收件人类型',
      dataIndex: 'action_subject',
      key: 'action_subject'
    },
    {
      title: '地址类型',
      dataIndex: 'type',
      key: 'type'
    },
    {
      title: '手机号码',
      dataIndex: 'mobile',
      key: 'mobile'
    },
    {
      title: '邮箱',
      dataIndex: 'email',
      key: 'email'
    },
    {
      title: '是否有效',
      dataIndex: 'status',
      key: 'status',
      render: (v: number) => {
        if (v === 1) {
          return '有效'
        }
        return '无效'
      }
    },
    {
      title: '操作',
      dataIndex: 'other',
      key: 'other',
      render: (v, l) => {
        if (!editable) {
          return false
        }
        return (
          <Space size='middle'>
            <span
              className={style.btn} onClick={() => {
                setModifyData(l)
                setVisible(true)
              }}
            >修改
            </span>
            <span className={classNames({ [style.btn]: l.status !== 1, [style['del-btn']]: l.status === 1 })} onClick={() => {
                confirmAction(`您确定要${l.status === 1 ? '禁用' : '启用'}该地址吗？`, () => {
                  updateHandle({id: l.id, status: l.status === 1 ? 0 : 1}, refreshFn)
                })
              }}
            >
              {l.status === 1 ? '禁用' : '启用'}
            </span>
          </Space>
        )
      }
    }
  ]
  return list
}